package com.qls.rabbitmq;

import com.qls.core.constant.Constant;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.AmqpException;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessageDeliveryMode;
import org.springframework.amqp.core.MessagePostProcessor;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * @author doyoung
 */
@Component
@Slf4j
public class SendMsg {

    /**
     * 消息不持久化
     */
    private final MessagePostProcessor messagePostProcessor = new MessagePostProcessor() {
        @Override
        public Message postProcessMessage(Message message) throws AmqpException {
            message.getMessageProperties().setDeliveryMode(MessageDeliveryMode.NON_PERSISTENT);
            return message;
        }
    };
    @Autowired
    private RabbitTemplate rabbitTemplate;

    /**
     * 发送价格更新消息
     *
     * @param rabbitMq RabbitMq
     * @param storeId  Integer
     * @param message  Object
     */
    public void execute(Constant.RabbitMq rabbitMq, Integer storeId, Object message) {
        String value = rabbitMq.getValue();
        String exchange = Constant.RabbitMq.HOTEL_EXCHANGE.getValue();
        switch (rabbitMq) {
            case RATES:
            case RP:
            case HOTEL:
                rabbitTemplate.convertAndSend(exchange, storeId + "_" + value, message, messagePostProcessor);
                break;
            case INIT:
            case CALLBACK_RATES:
            case CALLBACK_RP:
            case CALLBACK_HOTEL:
                rabbitTemplate.convertAndSend(exchange, value, message, messagePostProcessor);
                break;
            default:
                log.warn("发送消息路由错误");
                break;
        }
    }
}
